<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="th/include/common_head_nostyle :: head(~{::title}, ~{::link})">
    <title>重置密码</title>
    <link type="text/css" rel="stylesheet" th:href="@{${skinPath} + '/css.css'}"/>
</head>
<body>
<div class="container" style="margin-top:20px">
    <div class="row">
        <div class="col-lg-8 col-lg-offset-2">
            <div class="table-responsive">
                <form id="form1" method="post">
                <table class="table table-bordered table-hover" align="center">
                    <tr class="active">
                        <td colspan="2" align="center">请输入用户信息</td>
                    </tr>
                    <tr>
                        <td width="30%" align="right">用户名</td>
                        <td align="left" class="form-inline form-group">
                            <input id="userName" name="userName" class="form-control"/>
                        </td>
                    </tr>
                    <tr>
                        <td align="right">邮箱</td>
                        <td align="left" class="form-inline form-group">
                            <input id="email" name="email" class="form-control"/>
                            <span id="note"></span>
                            <script>
                                var lvUserName = new LiveValidation('userName');
                                lvUserName.add(Validate.Presence);
                                var lvEmail = new LiveValidation('email');
                                lvEmail.add(Validate.Presence);
                                lvEmail.add(Validate.Email, {failureMessage: '邮箱格式错误'});

                                $("#email").keydown(function (e) {
                                    var e = e || event,
                                        keycode = e.which || e.keyCode;
                                    if (keycode == 13) {
                                        setUserInfo();
                                    }
                                });
                            </script>
                        </td>
                    </tr>
                </table>
                <div class="text-center">
                    <button id="btnOk" class="btn btn-default">确定</button>
                    &nbsp;&nbsp;&nbsp;&nbsp;
                    <button id="btnBack" class="btn btn-default">返回</button>
                </div>
                </form>
            </div>
        </div>
    </div>
</div>
</body>
<script th:inline="javascript">
    $('#btnOk').click(function (e) {
        e.preventDefault();
        setUserInfo();
    });

    $('#btnBack').click(function (e) {
        e.preventDefault();
        window.location.href = '../index.do';
    });

    $(function() {
       $('#userName').change(function() {
           $.ajax({
               type: "post",
               url: "getUserEmailObscured.do",
               data: {
                   userName: $('#userName').val()
               },
               dataType: "json",
               beforeSend: function (XMLHttpRequest) {
                   $('body').showLoading();
               },
               success: function (data, status) {
                   if (data.ret == 1) {
                       $('#note').html("提示：" + data.email);
                   } else {
                       jAlert(data.msg, "提示");
                   }
               },
               complete: function (XMLHttpRequest, status) {
                   $('body').hideLoading();
               },
               error: function (XMLHttpRequest, textStatus) {
                   // 请求出错处理
                   alert(XMLHttpRequest.responseText);
               }
           });
       });
    });

    function setUserInfo() {
        if (!LiveValidation.massValidate(lvEmail.formObj.fields)) {
            return false;
        }
        $.ajax({
            type: "post",
            url: "resetPwdSendLink.do",
            contentType: "application/x-www-form-urlencoded; charset=iso8859-1",
            data: {
                userName: $('#userName').val(),
                email: $('#email').val()
            },
            dataType: "html",
            beforeSend: function (XMLHttpRequest) {
                $('body').showLoading();
            },
            success: function (data, status) {
                data = $.parseJSON(data);
                if (data.ret == 1) {
                    jAlert("操作成功，请进入邮箱点击重置密码链接", "提示", function () {
                        window.location.href = "../index.do";
                    });
                } else {
                    jAlert(data.msg, "提示");
                }
            },
            complete: function (XMLHttpRequest, status) {
                $('body').hideLoading();
            },
            error: function (XMLHttpRequest, textStatus) {
                // 请求出错处理
                alert(XMLHttpRequest.responseText);
            }
        });
    }
</script>
</html>